package com.cfpamf.athean.rabbit.mq.plus.domain.builder;

import com.cfpamf.athean.rabbit.mq.plus.common.constant.BusinessConstants;
import com.cfpamf.athean.rabbit.mq.plus.common.enums.ProcessStatusEnum;
import com.cfpamf.athean.rabbit.mq.plus.domain.dto.RabbitMqPlusEventTaskDTO;
import com.cfpamf.athean.rabbit.mq.plus.domain.dto.RabbitMqPlusMessageDTO;
import com.cfpamf.athean.rabbit.mq.plus.domain.po.RabbitMqPlusEventTaskPO;
import com.cfpamf.athena.utils.DateUtils;
import com.cfpamf.athena.utils.JSONUtils;
import com.cfpamf.athena.utils.ObjectUtils;

/**
 * 类RabbitMqPlusEventTaskBuilder.java的实现描述：
 *
 * @author Andy（陈洪桥）2022年11月26 16:08
 */
public class RabbitMqPlusEventTaskBuilder {

    public static RabbitMqPlusEventTaskDTO
        buildRabbitMqPlusEventTaskDTO(final RabbitMqPlusMessageDTO rabbitMqMessageDTO) {
        RabbitMqPlusEventTaskDTO eventTaskDTO = new RabbitMqPlusEventTaskDTO();
        eventTaskDTO.setEventNo(rabbitMqMessageDTO.getEventNo());
        eventTaskDTO.setEventTypeCode(rabbitMqMessageDTO.getEventTypeCode());
        eventTaskDTO.setEventTypeDesc(rabbitMqMessageDTO.getEventTypeDesc());
        eventTaskDTO.setStatusCode(ProcessStatusEnum.TODO.getCode());
        eventTaskDTO.setStatusDesc(ProcessStatusEnum.TODO.getDesc());
        eventTaskDTO.setMaxRetryCount(rabbitMqMessageDTO.getMaxRetryCount());
        eventTaskDTO.setRetryCount(0);
        eventTaskDTO.setMessageBody(JSONUtils.beanToJson(rabbitMqMessageDTO));
        eventTaskDTO.setEventTime(DateUtils.currentTime());
        // 下一次在5分钟后执行
        eventTaskDTO
            .setNextExecuteTime(DateUtils.convertDateToLocalDateTime(DateUtils.addMinute(BusinessConstants.FIVE)));
        return eventTaskDTO;
    }

    public static RabbitMqPlusEventTaskPO buildRabbitMqPlusEventTaskPO(RabbitMqPlusEventTaskDTO eventTaskDTO) {
        RabbitMqPlusEventTaskPO eventTaskPO = ObjectUtils.copyPropertys(eventTaskDTO, RabbitMqPlusEventTaskPO.class);
        eventTaskPO.setCreateTime(DateUtils.currentTime());
        eventTaskPO.setUpdateTime(eventTaskPO.getCreateTime());
        return eventTaskPO;
    }

}
